<?php
namespace Admin\Controller;

class EventsController extends AdminController {

	#列表
	function index() {	

		$keyword  = I('get.keyword');
		if($keyword!=''){
			$map['name'] = array('like', '%'.$keyword.'%');
		}

		$obj = M('Event');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('keyword', $keyword);
		$this->display();
	}

	#添加
	public function add() {
		$this->display();
	}

	#保存
	public function save() {
		$this->all_add('Event',U('Events/index'));
	}



	#编辑
	public function edit() {
		$id = (int) $_GET['id'];
		if (!isset ($id)) {
			$this->error('Parameter error');
		}

		$obj = M('Event');
		$list = $obj->where("id=$id")->find();
		if (!$list) {
			$this->error('Information does not exist');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}



	#更新
	public function update() {
		$this->all_save('Event',U('Events/index'));
	}

	#删除
	public function del() {
		$id=I('post.selectedids');
		if($id==''){
			$state = "You do not select any information!";
		}else{
			foreach ($id as $v) {
				$obj = M('Event');
				$where['id'] = $v;
				$list = $obj->field('picname')->where($where)->find();
				if ($list['picname']!='') {
					unlink(C('SITE_PATH').$list['picname']);
				}
				$obj->where($where)->delete();
			}
			$state = 'SUCCESS';
	        $message = 'Successful operation';
	        $url = U('Events/index');
		}
		echo $this->echo_json_str($state,$message,$url);
	}

	public function sign(){	
		$eventID = I('get.eventID');
		if ($eventID) {
			$map['eventID']=$eventID;
		}
		$obj = M('EventOrder');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$event = M('Event')->field('id,title')->order('id desc')->select();
		$this->assign('event',$event);
		$this->assign('eventID',$eventID);
		$this->display();
	}


	#开启
    public function open(){
        $map['id']=I('get.id');
        M('EventOrder')->where($map)->setField('status',1);
        redirect ($_SERVER['HTTP_REFERER']);
    }

    #关闭
    public function close(){
        $map['id']=I('get.id');
        M('EventOrder')->where($map)->setField('status',0);
        redirect ($_SERVER['HTTP_REFERER']);
    }	



    #开启
    public function openstudy(){
        $map['id']=I('get.id');
        M('EventOrder')->where($map)->setField('study',1);
        redirect ($_SERVER['HTTP_REFERER']);
    }



    #关闭
    public function closestudy(){
        $map['id']=I('get.id');
        M('EventOrder')->where($map)->setField('study',0);
        redirect ($_SERVER['HTTP_REFERER']);
    }	



    public function detail(){
    	$id = I('get.id');
        $map['id'] = $id;
        $list = M('EventOrder')->where($map)->find();
        if (!$list) {
            $this->error('Information does not exist');
        } else {
            $this->assign('list', $list);
            $this->display();
        }
    }



    //更新order

    public function updateorder(){
        $obj = D('EventOrder');
        if ($data = $obj->create()) {
            if ($list = $obj->save($data)) {
                $this->success(L('_action_success'));
            } else {
                $this->success(L('_action_fail'));
            }
        } else {
            $this->success($obj->getError());
        }
    }



	#删除
	public function delsign() {
		$id=I('post.selectedids');
		if($id==''){
			$state = "You do not select any information!";
		}else{
			foreach ($id as $v) {
				$obj = M('EventOrder');
				$where['id'] = $v;
				$obj->where($where)->delete();
			}

			$state = 'SUCCESS';
	        $message = 'Successful operation';
	        $url = U('Events/sign');
		}
		echo $this->echo_json_str($state,$message,$url);
	}
}
?>